package com.soso.confidant.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.soso.confidant.pojo.Amass;
import com.soso.confidant.service.TasteServiceImpl;
import com.soso.confidant.util.ParseString;
import com.soso.confidant.util.Remote;
import com.soso.confidant.util.Replace;
import static java.net.URLDecoder.*; 

public class QAction implements ServletRequestAware,ServletResponseAware{
	
	private HttpServletRequest request;
	private HttpServletResponse response;
	private String w;
	private int pg = 1;
	
	public String execute() throws IOException {
		response.setContentType("text/html;charset=GB2312");
        PrintWriter out = response.getWriter();
        
		//得到关键字w和页码pg
		String queryString = request.getQueryString();
//		System.out.println(queryString);
//		queryString = decode(queryString,"GB2312");
//		queryString = queryString.trim();
//		queryString = queryString.replaceAll("\\s+", "+");
		
		String url ="http://www.soso.com/q?"+queryString;
		
		Remote remote = Remote.getInstance();
		Replace replace = Replace.getInstance();
		String text = remote.getRemotePage(url, "GB2312");
		
        //把相关搜搜，分页等请求/q?替换成q?,其实可正常跳转至本action
        String linkedresult =replace.PageLinkReplace(text);
        
		String openid = (String)request.getSession().getAttribute("openid");
		w = new String(w.getBytes("ISO8859-1"),"GB2312");
		w = w.trim();
		w = w.replaceAll("\\s+", "+");
//		System.out.println(w);
		ArrayList<String> words = ParseString.ParseWordString(w);
		TasteServiceImpl ts = TasteServiceImpl.getInstance();
		//System.out.println(openid);
		if(openid!=null&&pg==1){
			ArrayList<Amass>  amasses = ts.getAmasses(words, openid, 6);
			String loc0 = getloc0(amasses);
			linkedresult = linkedresult.replace("<ol  >", "<ol  >"+loc0);
		}
		
        //System.out.println(linkedresult);
        //得到对用户的输出流，并输出
        
		out.print(linkedresult);
		out.flush();
		out.close();
		return null;
	}
	public String getloc0(ArrayList<Amass>  amasses){
		String loc0 ="";
		if(amasses.size()>0){
			loc0 = "<li loc='0'><h2>你是不是在找：</h2><p style='margin:0 0 0 25px;'>";
			for(int i =0;i<amasses.size();i++){
				Amass amass = amasses.get(i);
				loc0 += "<a style='line-height:150%;' href='"+amass.getHref()+"'>"+amass.getTitle()+"</a>来自：腾讯首页www.qq.com</br>";
			}
			loc0 += "</p></li>";
		}
		return loc0;
	}
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public String getW() {
		return w;
	}

	public void setW(String w) {
		this.w = w;
	}

	public int getPg() {
		return pg;
	}

	public void setPg(int pg) {
		this.pg = pg;
	}
	
}
